package service

import (
	"context"
	"strconv"

	"github.com/go-kratos/kratos/v2/metadata"
)

type AuthUser struct {
	AppId          int32
	MerchantId     int32
	MerchantUserId int32
}

func GetAuthUser(ctx context.Context) (auth AuthUser) {
	if md, ok := metadata.FromServerContext(ctx); ok {
		if id, ok := md["x-md-global-am-id"]; ok {
			mid, _ := strconv.Atoi(id)
			auth.MerchantId = int32(mid)
		}
		if id, ok := md["x-md-global-am-uid"]; ok {
			uid, _ := strconv.Atoi(id)
			auth.MerchantUserId = int32(uid)
		}
		if id, ok := md["x-md-global-app-id"]; ok {
			appId, _ := strconv.Atoi(id)
			auth.AppId = int32(appId)
		}
	}
	return
}
